package client.mobile;


import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;

import client.main.Writer;
import client.utility.Id;
import client.utility.Point2d;
import client.utility.PacketId.PacketID;



public class Player {
	private Writer writer;
	private HashMap<Id,Mobile> visibleMob;
	private Id mapId;
	private Id id;
	private String name;
	private Point2d position;
	private BufferedReader reader;
	private int health;
	private int maxHealth;
	
	public static Player constructFromString(String s){
		String[] split=s.split(";");
		Player res=new Player();
		
		try{
			res.name=split[0];
			res.id=new Id(Integer.parseInt(split[2]));
			res.position=Point2d.fromString(split[1]);
			res.mapId=new Id(Integer.parseInt(split[5]));
			res.health = Integer.parseInt(split[3]);
			res.maxHealth = Integer.parseInt(split[4]);
		return res;
		}catch(Exception ex){return null;}
	}
	
	private Player(){
		visibleMob=new HashMap<Id,Mobile>();
	}

	public void add(Mobile mob) {
		visibleMob.put(mob.getId(), mob);
	}
	
	public Set<Id> getVisibleModId(){
		return visibleMob.keySet();
	}
	
	public Collection<Mobile> getVisibleMob(){
		return visibleMob.values();
	}
	
	public Point2d getPosition(){
		return position;
	}
	
	public void setPosition(Point2d pos){
		position = pos;
	}
	
	public String getName(){
		return name;
	}

	public Id getId() {
		return id;
	}

	public Id getMap() {
		return mapId;
	}
	

	public void updatePositions(String[] positions) {
		
		ArrayList<Id> setted=new ArrayList<Id>();
		
		this.mapId=new Id(Integer.parseInt(positions[0]));
		
		for(int i = 1; i < positions.length; i++){
			try{
				String[] tmp=positions[i].split("@");
				Id id=new Id(Integer.parseInt(tmp[0]));
				Point2d p=Point2d.fromString(tmp[1]);
				String[] tmp1 = tmp[1].split(",");
				int health = Integer.parseInt(tmp1[2]);
				int maxHealth = Integer.parseInt(tmp1[3]);
				
				if(id.equals(this.id)){
					position=p;
					this.health = health;
					this.maxHealth = maxHealth;
				}
				else if(visibleMob.containsKey(id)){
					Mobile tmpMob = visibleMob.get(id);
					tmpMob.setPosition(p);
					tmpMob.setHealth(health);
					tmpMob.setMaxHealth(maxHealth);
					setted.add(id);
				}
				else{
					writer.write(PacketID.mobsDetails, id);
					
					visibleMob.put(id, Mobile.constructFromString(reader.readLine()));
					setted.add(id);
				}
				
			}catch(Exception ex){
				System.out.println("exception in update position");
			}
		}
		
		ArrayList<Id> toRemove=new ArrayList<Id>();
		for(Id id:visibleMob.keySet()){
			if(!setted.contains(id))
				toRemove.add(id);
		}
		
		for(Id id:toRemove)
			visibleMob.remove(id);
	}

	public void setWriter(Writer writer) {
		this.writer=writer;
	}

	public void setReader(BufferedReader reader) {
		this.reader=reader;
	}

	public Writer getWriter() {
		return writer;
	}

	public BufferedReader getReader() {
		return reader;
	}

	public int getMaxHealth() {
		return maxHealth;
	}

	public double getHealth() {
		return health;
	}

}
